{-# LANGUAGE OverloadedStrings #-}

module Util
    ( prelude
    , printdoc
    , vw
    , vh
    , textblock
    , textblock'
    , rotatebox'
    , fontsize
    ) where

import Data.Text.Encoding
import qualified Data.ByteString as B
import Text.LaTeX
import Text.LaTeX.Base.Class
import Text.LaTeX.Base.Syntax

printdoc x = execLaTeXT x >>= B.putStr . encodeUtf8 . render

prelude :: Monad m => LaTeXT_ m
prelude = do
    documentclass [Paper A4] article
    usepackage [] "graphicx"
    usepackage [] "geometry"
    usepackage [] "lmodern"
    usepackage ["absolute", "overlay"] "textpos"
    pagenumbering "gobble"

paperwidth :: LaTeXC l => l
paperwidth = commS "paperwidth"

paperheight :: LaTeXC l => l
paperheight = commS "paperheight"

vw :: LaTeXC l => Double -> l
vw x = rendertex x <> commS "paperwidth"

vh :: LaTeXC l => Double -> l
vh y = rendertex y <> commS "paperheight"

tw :: LaTeXC l => Double -> l
tw x = rendertex x <> commS "textwidth"

th :: LaTeXC l => Double -> l
th y = rendertex y <> commS "textheight"

textblock :: LaTeXC l => LaTeX -> LaTeX -> LaTeX -> l -> l
textblock w x y = liftL $ TeXEnv "textblock*" [
    FixArg $ w,
    MOptArg [rendertex (0.5 :: Double) , rendertex (0.5 :: Double)],
    MParArg [x , y]]

textblock' :: LaTeXC l => LaTeX -> LaTeX -> l -> l
textblock' x y z = textblock (commS "paperwidth") x y $ center z

rotatebox' :: LaTeXC l => Double -> l -> l
rotatebox' a = liftL $ \l -> TeXComm "rotatebox" [OptArg "origin=c", FixArg $ rendertex a, FixArg l]

fontsize :: LaTeXC l => l -> l -> l -> l
fontsize x y z = braces $ comm2 "fontsize" x y <> comm0 "selectfont" <> z
